import {
	Viewer,
	ScreenSpaceEventHandler,
	ScreenSpaceEventType
} from 'cesium'

import CountryView from '../CountryView'

import Baseplate from './Baseplate'
import DistrictBlock from './DistrictBlock'
import RangeBlock from './RangeBlock'

class ProvinceView {
	
	viewer: Viewer | null = null
	isLoad: boolean = false
	isGrowing: boolean = false
	
	countryView: CountryView | null = null
	
	baseplate: Baseplate | null = null
	districtBlock: DistrictBlock | null = null
	rangeBlock: RangeBlock | null = null
	
	rightClickHandler: ScreenSpaceEventHandler | null
	
	
	constructor(viewer: Viewer) {
	    this.viewer = viewer
		
		this.baseplate = new Baseplate(viewer)
		this.districtBlock = new DistrictBlock(viewer)
		this.rangeBlock = new RangeBlock(viewer)
	}
	
	async load(code: string) {
		if(this.isGrowing) return
		this.isGrowing = true
		
		if(this.isLoad) return
		this.isLoad = true
		
		await Promise.all([
			this.baseplate.load(code),
			this.districtBlock.load(code),
			this.rangeBlock.load(code),
			this.countryView.hide()
		])

		this.rightClickHandler = new ScreenSpaceEventHandler()
		this.rightClickHandler.setInputAction(async ()=>{
			this.unload()
			this.countryView.show()
		}, ScreenSpaceEventType.RIGHT_CLICK)
		this.isGrowing = false
	}
	
	async loadCountryView(countryView: CountryView) {
		this.countryView = countryView
	}
	
	async unload() {
		if(this.isGrowing) return
		this.isGrowing = true
		
		if(this.isLoad == false) return
		this.isLoad = false
		
		this.rightClickHandler.removeInputAction(ScreenSpaceEventType.RIGHT_CLICK)
		await Promise.all([
			this.baseplate.unload(),
			this.districtBlock.unload(),
			this.rangeBlock.unload()
		])
		this.isGrowing = false
	}
}
export default ProvinceView